package cn.itcast.jk.controller.cargo.contract;

import cn.itcast.jk.controller.BaseController;
import cn.itcast.jk.domain.ExtCproduct;
import cn.itcast.jk.domain.Factory;
import cn.itcast.jk.domain.SysCode;
import cn.itcast.jk.service.ExtCproductService;
import cn.itcast.jk.service.FactoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
public class ExtCproductController extends BaseController {
    @Autowired
    ExtCproductService extCproductService;
    @Autowired
    FactoryService factoryService;
    @RequestMapping("/cargo/ExtCproduct/toinsert.action")
    public String toinsert(String contractProductId, Model model, String contractId){      //准备数据转向新增页面
        model.addAttribute("contractProductId", contractProductId);     //传递合同货物ID
        model.addAttribute("contractId", contractId);     //传递合同ID
        //准备附件列表
        Map map = new HashMap();
        map.put("contractProductId",contractProductId);
        List<ExtCproduct> dataList = extCproductService.find(map);
        model.addAttribute("dataList",dataList);
        //准备生产厂家下拉列表
        List<Factory> factoryList = factoryService.getListFactory();
        model.addAttribute("factoryList",factoryList);
        //准备附件类型下拉列表
        List<SysCode> ctypeList = extCproductService.getCtype();
        model.addAttribute("ctypeList",ctypeList);
        return "/cargo/contract/jExtCproductCreate.jsp";
    }
    @RequestMapping("/cargo/ExtCproduct/insert.action")               //新增保存
    public String insert(ExtCproduct extCproduct, Model model,String contractId){
        extCproductService.insert(extCproduct);
        model.addAttribute("contractProductId", extCproduct.getContractProductId());    //传递合同货物ID
        model.addAttribute("contractId", contractId);     //传递合同ID
        return "redirect:/cargo/ExtCproduct/toinsert.action";
    }

    @RequestMapping("/cargo/ExtCproduct/toupdate.action")
    public String toupdate(Model model,String id, String contractProductId,String contractId){          //转向修改页面
        model.addAttribute("contractProductId", contractProductId);     //传递合同货物ID
        model.addAttribute("contractId", contractId);     //传递合同ID
        ExtCproduct extCproduct = extCproductService.get(id);
        model.addAttribute("obj",extCproduct);
        //准备生产厂家下拉列表
        List<Factory> factoryList = factoryService.getListFactory();
        model.addAttribute("factoryList",factoryList);
        //准备附件类型下拉列表
        List<SysCode> ctypeList = extCproductService.getCtype();
        model.addAttribute("ctypeList",ctypeList);
        return "/cargo/contract/jExtCproductUpdate.jsp";
    }
    @RequestMapping("/cargo/ExtCproduct/update.action")
    public String update(ExtCproduct extCproduct,String contractProductId,String contractId,Model model){           //修改保存
        model.addAttribute("contractProductId", contractProductId);     //传递合同货物ID
        model.addAttribute("contractId", contractId);     //传递合同ID
        extCproductService.update(extCproduct);
        return "redirect:/cargo/ExtCproduct/toinsert.action";
    }
    @RequestMapping("/cargo/ExtCproduct/delete.action")             //删除
    public String delete(String id,Model model,String contractProductId,String contractId){
        extCproductService.deleteById(id);
        model.addAttribute("contractProductId", contractProductId);    //传递合同货物ID
        model.addAttribute("contractId", contractId);     //传递合同ID
        return "redirect:/cargo/ExtCproduct/toinsert.action";
    }
}
